package halberd.utils;

public class ConvolutionUtils {
	public static void convolution9(int[][] op, int[][][] src, int[][][] dst, int w, int h) {
		for (int i = 0; i < w; ++i) {
			for (int j = 0; j < h; ++j) {
				int sumR=0;
				int sumG=0;
				int sumB=0;
				int div=0;
				for(int m=0,m0=-1;m<3;++m,++m0) {
					for(int n=0,n0=-1;n<3;++n,++n0) {
						int i0=i+m0;
						int j0=j+n0;
						if(i0<0||i0>=w||j0<0||j0>=h) {
							continue;
						}
						sumR+=op[m][n]*src[i0][j0][0];
						sumG+=op[m][n]*src[i0][j0][1];
						sumB+=op[m][n]*src[i0][j0][2];
						div+=op[m][n];
					}
				}
				if(div>0) {
					sumR=sumR/div;
					sumG=sumG/div;
					sumB=sumB/div;
				}
				dst[i][j][0]=sumR;
				dst[i][j][1]=sumG;
				dst[i][j][2]=sumB;
			}
		}
	}
}
